﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;
using System.IO;
using Microsoft.Xna.Framework.Input;

namespace TerenXNA
{
    class Slider
    {
        Rectangle barRectangle;
        Rectangle sliderRectangle;
        private int width;
        private int x, y;
        private float min = -5.0f;
        private float max = 5.0f;
        private Texture2D texture;
        private Color backgroundColor;
        private bool pressed = false;
        private int positionZero;

        public Slider(int width, int x, int y, Texture2D text)
        {
            this.width = width;
            this.x = x;
            this.y = y;
            this.texture = text;

            barRectangle = new Rectangle(x, y, width, 5);
            sliderRectangle = new Rectangle((x + width / 2)-10, y - 20, 20, 40);
            this.backgroundColor = Color.Silver;
            positionZero = (x + width / 2)-10;
        }

        public void MouseUpdate(MouseState mouseState)
        {
            if (sliderRectangle.Contains(new Point(mouseState.X, mouseState.Y)) && mouseState.LeftButton == ButtonState.Pressed)
            {
                pressed = true;
            }
            else if(mouseState.LeftButton == ButtonState.Released)
            {
                pressed = false;
            }

            if (pressed && mouseState.LeftButton == ButtonState.Pressed)
            {
                if (mouseState.X >= barRectangle.X  && mouseState.X <= barRectangle.X + width)
                {
                    sliderRectangle.X = mouseState.X - 10;
                }
            }
            
        }

        public float GetValue()
        {
            return (float)(((sliderRectangle.X) - positionZero)) * ((max - min) / width);
        }

        public void ForceValue(float XValue)
        {
            sliderRectangle.X = (int)(XValue * width / (max - min)) + positionZero;
            //Console.WriteLine(GetValue());
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(texture, this.barRectangle, backgroundColor);
            sb.Draw(texture, this.sliderRectangle, Color.Red);
        }

    }
}
